package function;

import View.*;
import client.ClientManager;
import instruction.Message;
import instruction.User;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;


import java.io.IOException;
import java.util.regex.Pattern;

import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.shape.Circle;

public class FillInformationFunction {
    private static String imagePath = "/Picture/headPortrait/喜羊羊.jpeg";

    public static Object $(window window, String id) {
        return (Object) window.getRoot().lookup("#" + id);
    }

    public static void fillInformationExec(FillInformationWindow fillInformationWindow, LoginWindow loginWindow, RegisterWindow registerWindow, SelectHeadWindow selectHeadWindow, ClientManager clientManager) throws IOException, ClassNotFoundException {

        ((ImageView) $(fillInformationWindow, "headImage")).setImage(new Image(imagePath, 80, 80, false, false));
        ((ImageView) $(fillInformationWindow, "headImage")).setClip(getCircle());
        ((Button) $(fillInformationWindow, "goButton")).setOnAction(actionEvent -> {
            fillInformationWindow.resetErrorTip();
            String correctName = "[\u4E00-\u9FA5]{2,5}(?:·[\u4E00-\u9FA5]{2,5})*";
            //String correctSex = "/^男$|^女&/";
            String correctAge = "^(?:[1-9][0-9]?|1[01][0-9]|120)$";
            String correctNickname = "^(?!_)(?!.*?_$)[a-zA-Z0-9_\\u4e00-\\u9fa5]+$";
            // String correctPhone = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\\\d{8}$";
            String correctBirthday = "((^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._])(10|12|0?[13578])([-\\/\\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._])(11|0?[469])([-\\/\\._])(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._])(0?2)([-\\/\\._])(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([3579][26]00)([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([1][89][0][48])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([2-9][0-9][0][48])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([1][89][2468][048])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([2-9][0-9][2468][048])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([1][89][13579][26])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([2-9][0-9][13579][26])([-\\/\\._])(0?2)([-\\/\\._])(29)$))";
            String correctAddress = "([^省]+省|.+自治区|[^市]+市)([^自治州]+自治州|[^市]+市|[^盟]+盟|[^地区]+地区|.+区划)([^市]+市|[^县]+县|[^旗]+旗|.+区)";
            String userName = ((TextField) $(fillInformationWindow, "nameText")).getText();
            String userSex = ((TextField) $(fillInformationWindow, "sexText")).getText();
            String userBirthday = ((TextField) $(fillInformationWindow, "birthdayText")).getText();
            String userAge = ((TextField) $(fillInformationWindow, "ageText")).getText();
            String userAddress = ((TextField) $(fillInformationWindow, "addressText")).getText();
            String userPhone = ((TextField) $(fillInformationWindow, "phoneText")).getText();
            String userNickname = ((TextField) $(fillInformationWindow, "nicknameText")).getText();
            String userSign = ((TextField) $(fillInformationWindow, "signText")).getText();
            if (userName.equals("") || userSex.equals("") || userBirthday.equals("") || userAge.equals("") || userAddress.equals("") || userPhone.equals("") || userNickname.equals("") || userSign.equals("")) {
                if (userName.equals("")) {
                    fillInformationWindow.setErrorTip("nameTip", "姓名不能为空");
                }
                if (userSex.equals("")) {
                    fillInformationWindow.setErrorTip("sexTip", "性别不能为空");
                }
                if (userBirthday.equals("")) {
                    fillInformationWindow.setErrorTip("birthdayTip", "生日不能为空");
                }
                if (userAge.equals("")) {
                    fillInformationWindow.setErrorTip("ageTip", "年龄不能为空");
                }
                if (userAddress.equals("")) {
                    fillInformationWindow.setErrorTip("addressTip", "地址不能为空");
                }
                if (userPhone.equals("")) {
                    fillInformationWindow.setErrorTip("phoneTip", "手机号码不能为空");
                }
                if (userNickname.equals("")) {
                    fillInformationWindow.setErrorTip("nicknameTip", "昵称不能为空");
                }
                if (userSign.equals("")) {
                    fillInformationWindow.setErrorTip("signTip", "个性签名不能为空");
                }
            } else if (!Pattern.matches(correctName, userName) || /*!Pattern.matches(correctSex, userSex) ||*/ !Pattern.matches(correctBirthday, userBirthday) || !Pattern.matches(correctAge, userAge) || !Pattern.matches(correctAddress, userAddress) || /*!Pattern.matches(correctPhone, userPhone) || */!Pattern.matches(correctNickname, userNickname)) {
                if (!Pattern.matches(correctName, userName)) {
                    fillInformationWindow.setErrorTip("nameTip", "姓名格式不对");
                }
               /* if (!Pattern.matches(correctSex, userSex)) {
                    fillInformationWindow.setErrorTip("sexTip", "性别格式不对");
                }*/
                if (!Pattern.matches(correctBirthday, userBirthday)) {
                    fillInformationWindow.setErrorTip("birthdayTip", "生日格式不对");
                }
                if (!Pattern.matches(correctAge, userAge)) {
                    fillInformationWindow.setErrorTip("ageTip", "年龄格式不对");
                }
                if (!Pattern.matches(correctAddress, userAddress)) {
                    fillInformationWindow.setErrorTip("addressTip", "地址格式不对");
                }
               /* if (!Pattern.matches(correctPhone, userPhone)) {
                    fillInformationWindow.setErrorTip("phoneTip", "手机号码格式不对");
                }*/
                if (!Pattern.matches(correctNickname, userNickname)) {
                    fillInformationWindow.setErrorTip("nicknameTip", "昵称格式不对");
                }
            } else {
                String userAccount = ((TextField) $(registerWindow, "actText")).getText();
                String userPassword = ((PasswordField) $(registerWindow, "pswText")).getText();
                String userEmail = ((TextField) $(registerWindow, "emailText")).getText();
                Message message = new Message("register");
                message.setRegister(new User(userName, userAccount, userPassword, userSex, userBirthday, userSign, userEmail, userAge, userAddress, userNickname, userPhone, imagePath));

                clientManager.getSend().send(message);
                fillInformationWindow.hide();
                loginWindow.show();
            }
        });
        ((Button) $(fillInformationWindow, "imageButton")).setOnAction(event -> {
            selectHeadWindow.InitHead();
            selectHeadWindow.show();
            ((Button) $(selectHeadWindow, "happyButton")).setOnAction(actionEvent -> {
                imagePath = "/Picture/headPortrait/喜羊羊.jpeg";
                ((ImageView) $(fillInformationWindow, "headImage")).setImage(new Image(imagePath, 80, 80, false, false));
                ((ImageView) $(fillInformationWindow, "headImage")).setClip(getCircle());
                selectHeadWindow.hide();
            });
            ((Button) $(selectHeadWindow, "beautifulButton")).setOnAction(actionEvent -> {
                imagePath = "/Picture/headPortrait/美羊羊.jpg";
                ((ImageView) $(fillInformationWindow, "headImage")).setImage(new Image(imagePath, 80, 80, false, false));
                ((ImageView) $(fillInformationWindow, "headImage")).setClip(getCircle());
                selectHeadWindow.hide();
            });
            ((Button) $(selectHeadWindow, "excitedButton")).setOnAction(actionEvent -> {
                imagePath = "/Picture/headPortrait/沸羊羊.jpg";
                ((ImageView) $(fillInformationWindow, "headImage")).setImage(new Image(imagePath, 80, 80, false, false));
                ((ImageView) $(fillInformationWindow, "headImage")).setClip(getCircle());
                selectHeadWindow.hide();
            });
            ((Button) $(selectHeadWindow, "lazyButton")).setOnAction(actionEvent -> {
                imagePath = "/Picture/headPortrait/懒羊羊.jpg";
                ((ImageView) $(fillInformationWindow, "headImage")).setImage(new Image(imagePath, 80, 80, false, false));
                ((ImageView) $(fillInformationWindow, "headImage")).setClip(getCircle());
                selectHeadWindow.hide();
            });
            ((Button) $(selectHeadWindow, "warmButton")).setOnAction(actionEvent -> {
                imagePath = "/Picture/headPortrait/暖羊羊.jpg";
                ((ImageView) $(fillInformationWindow, "headImage")).setImage(new Image(imagePath, 80, 80, false, false));
                ((ImageView) $(fillInformationWindow, "headImage")).setClip(getCircle());
                selectHeadWindow.hide();
            });
            ((Button) $(selectHeadWindow, "villageButton")).setOnAction(actionEvent -> {
                imagePath = "/Picture/headPortrait/村长.jpg";
                ((ImageView) $(fillInformationWindow, "headImage")).setImage(new Image(imagePath, 80, 80, false, false));
                ((ImageView) $(fillInformationWindow, "headImage")).setClip(getCircle());
                selectHeadWindow.hide();
            });
            ((Button) $(selectHeadWindow, "grayButton")).setOnAction(actionEvent -> {
                imagePath = "/Picture/headPortrait/灰太狼.jpeg";
                ((ImageView) $(fillInformationWindow, "headImage")).setImage(new Image(imagePath, 80, 80, false, false));
                ((ImageView) $(fillInformationWindow, "headImage")).setClip(getCircle());
                selectHeadWindow.hide();
            });
            ((Button) $(selectHeadWindow, "redButton")).setOnAction(actionEvent -> {
                imagePath = "/Picture/headPortrait/红太狼.jpg";
                ((ImageView) $(fillInformationWindow, "headImage")).setImage(new Image(imagePath, 80, 80, false, false));
                ((ImageView) $(fillInformationWindow, "headImage")).setClip(getCircle());
                selectHeadWindow.hide();
            });
            ((Button) $(selectHeadWindow, "bananaButton")).setOnAction(actionEvent -> {
                imagePath = "/Picture/headPortrait/蕉太狼.jpeg";
                ((ImageView) $(fillInformationWindow, "headImage")).setImage(new Image(imagePath, 80, 80, false, false));
                ((ImageView) $(fillInformationWindow, "headImage")).setClip(getCircle());
                selectHeadWindow.hide();
            });
            ((Button) $(selectHeadWindow, "smallButton")).setOnAction(actionEvent -> {
                imagePath = "/Picture/headPortrait/小灰灰.jpg";
                ((ImageView) $(fillInformationWindow, "headImage")).setImage(new Image(imagePath, 80, 80, false, false));
                ((ImageView) $(fillInformationWindow, "headImage")).setClip(getCircle());
                selectHeadWindow.hide();
            });
        });
    }

    public static Circle getCircle() {
        Circle circle = new Circle();
        circle.setRadius(40);
        circle.setCenterX(40);
        circle.setCenterY(40);
        return circle;
    }
}